// 指向-计时
interface operationSet {
    peng: boolean, 
    gang: boolean, 
    gonggang: boolean,
    hu: boolean, 
    guo: boolean, 
    chi: boolean, 
    qishouhu: boolean, 
    gangyaoshaizi: boolean, 
    haidiyaoshaizi: boolean
    bu: boolean,
    baoting: boolean,
    angang: boolean
}
class operationBtn extends eui.Component{
    private btn_guo:eui.Image;
    private actionCard:any;
    public multi: boolean = false
    private cardobj:handPoker
    public winMsg: any
    public candoActions: any
    public constructor(_actionCard:any = null){
        super();
        this.actionCard = _actionCard;
        this.once(eui.UIEvent.COMPLETE,this.onComplete,this);
        this.skinName = "src/Game/gameRoom/DIYBtn/operationBtnSkin.exml";
    }
    private onComplete(){
        if(this.cardobj){
            this.removeChild(this.cardobj)
            this.cardobj = null;
        }
        this.once(egret.Event.REMOVED_FROM_STAGE,this.onRemove,this);
        this.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onClick,this);
    }
    private onRemove(){
        this.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onClick,this);        
    }
    private onClick(e:egret.TouchEvent){
        switch(e.target.name){
            case "btn_peng":
            this.dispatchEventWith(PokerEvent.WIN_POKER,false,"peng");
            break;

            case "btn_gang":
            this.dispatchEventWith(PokerEvent.WIN_POKER,false,"gang");
            break;

            case "btn_hu":
            this.dispatchEventWith(PokerEvent.WIN_POKER,false,"hu");
            break;

            case "btn_guo":
            this.dispatchEventWith(PokerEvent.WIN_POKER,false,"guo");
            break;
            case "btn_chi":
            this.dispatchEventWith(PokerEvent.WIN_POKER,false,"chi");
            break;
            case "btn_qishouhu":
            this.dispatchEventWith(PokerEvent.WIN_POKER,false,"qishouhu");
            break;
            case "btn_gangyao":
            this.dispatchEventWith(PokerEvent.WIN_POKER,false,"gangyao");
            break;
            case "btn_haidiyao":
            this.dispatchEventWith(PokerEvent.WIN_POKER,false,"haidiyao");
            break;
            case 'btn_bu':
            this.dispatchEventWith(PokerEvent.WIN_POKER,false,"bu");
            break;
            case 'btn_gonggang':
            this.dispatchEventWith(PokerEvent.WIN_POKER,false,"gonggang");
            break;
            case 'btn_angang':
            this.dispatchEventWith(PokerEvent.WIN_POKER,false,"angang");
            break;
            case 'btn_baoting':
            this.dispatchEventWith(PokerEvent.WIN_POKER,false,"baoting");
            break;
        }
    }

    private btnContentBg:egret.Sprite;
    public setWinPokerMsg(msg) {
        this.winMsg = msg
        console.log(this.winMsg);
    }
    // peng:boolean,gang:boolean,hu:boolean,guo:boolean, chi: boolean
    public setContent(operationSet: operationSet){
        console.log('operationSet', operationSet);
        const {peng, gang, gonggang, angang, hu, guo, chi, qishouhu, gangyaoshaizi, haidiyaoshaizi, bu, baoting} = operationSet
        console.log('setContent', chi);
        this.btn_guo.alpha = 1;
        this.btn_guo.visible = true;
        this.btn_guo.touchEnabled = true;

        if(this.btnContentBg){
            this.removeChild(this.btnContentBg);
            this.btnContentBg = null;
        }
        this.btnContentBg = new egret.Sprite();
        this.addChild(this.btnContentBg);
        const canNotBu = GameData.myUserData.listenCard === 3
        console.log('canNotBu', canNotBu);
        if(gang && !canNotBu){
            // var btn2:egret.Bitmap = MyLibrary.createBitmapByName("btn_gang_png");
            var btn2:egret.Bitmap = MyLibrary.createBitmapByName("bububu_png");
            btn2.name = "btn_gang";
            btn2.touchEnabled = true;
            btn2.x = this.btnContentBg.width;
            this.btnContentBg.addChild(btn2);
        }
        // if ((gonggang || angang) && !canNotBu) {
        //     // var btn2:egret.Bitmap = MyLibrary.createBitmapByName("btn_gang_png");
        //     var btn22:egret.Bitmap = MyLibrary.createBitmapByName("bububu_png");
        //     btn22.name = "btn_gonggang";
        //     btn22.touchEnabled = true;
        //     btn22.x = this.btnContentBg.width;
        //     this.btnContentBg.addChild(btn22);
        // }
        if (gonggang && !canNotBu) {
            // var btn2:egret.Bitmap = MyLibrary.createBitmapByName("btn_gang_png");
            var btn22:egret.Bitmap = MyLibrary.createBitmapByName("bububu_png");
            btn22.name = "btn_gonggang";
            btn22.touchEnabled = true;
            btn22.x = this.btnContentBg.width;
            this.btnContentBg.addChild(btn22);
        }
        if (angang && !canNotBu) {
            // var btn2:egret.Bitmap = MyLibrary.createBitmapByName("btn_gang_png");
            var btn222:egret.Bitmap = MyLibrary.createBitmapByName("bububu_png");
            btn222.name = "btn_angang";
            btn222.touchEnabled = true;
            btn222.x = this.btnContentBg.width;
            this.btnContentBg.addChild(btn222);
        }
        if(peng){
            var btn1:egret.Bitmap = MyLibrary.createBitmapByName("btn_peng_png");
            btn1.name = "btn_peng";
            btn1.touchEnabled = true;
            btn1.x = this.btnContentBg.width;
            this.btnContentBg.addChild(btn1);
        }        
        if(hu){
            var btn3:egret.Bitmap = MyLibrary.createBitmapByName("btn_hu_png");
            btn3.name = "btn_hu";
            btn3.touchEnabled = true;
            btn3.x = this.btnContentBg.width;
            this.btnContentBg.addChild(btn3);
        }
        if(chi){
            var btn4:egret.Bitmap = MyLibrary.createBitmapByName("eat_png");
            btn4.name = "btn_chi";
            btn4.touchEnabled = true;
            btn4.x = this.btnContentBg.width;
            this.btnContentBg.addChild(btn4);
        }  

        // ----------------- new 
        if(qishouhu){
            var btn5:egret.Bitmap = MyLibrary.createBitmapByName("qishouhu_png");
            btn5.name = "btn_qishouhu";
            btn5.touchEnabled = true;
            btn5.x = this.btnContentBg.width;
            this.btnContentBg.addChild(btn5);
        } 
        if(gangyaoshaizi){
            var btn6:egret.Bitmap = MyLibrary.createBitmapByName("dami-icon_png");
            btn6.name = "btn_gangyao";
            btn6.touchEnabled = true;
            btn6.x = this.btnContentBg.width;
            this.btnContentBg.addChild(btn6);
        } 
        if(haidiyaoshaizi){
            var btn7:egret.Bitmap = MyLibrary.createBitmapByName("haidi-icon_png");
            btn7.name = "btn_haidiyao";
            btn7.touchEnabled = true;
            btn7.x = this.btnContentBg.width;
            this.btnContentBg.addChild(btn7);
        } 
        if(bu){
            // var btn8:egret.Bitmap = MyLibrary.createBitmapByName("bububu_png");
            var btn8:egret.Bitmap = MyLibrary.createBitmapByName("dami-icon_png");            
            btn8.name = "btn_bu";
            btn8.touchEnabled = true;
            btn8.x = this.btnContentBg.width;
            this.btnContentBg.addChild(btn8);
        } 
        if(baoting){
            // var btn8:egret.Bitmap = MyLibrary.createBitmapByName("bububu_png");
            var btn9:egret.Bitmap = MyLibrary.createBitmapByName("baoting-icon_png");
            btn9.name = "btn_baoting";
            btn9.touchEnabled = true;
            btn9.x = this.btnContentBg.width;
            this.btnContentBg.addChild(btn9);
        } 
        
        this.btn_guo.x = this.btnContentBg.width;
        console.log('this.multi', this.multi);
        if(this.actionCard && !baoting && !this.multi){
            this.cardobj = new handPoker();
            this.cardobj.scaleX = this.cardobj.scaleY = 0.6;
            this.cardobj.setContent(this.actionCard.suitType+"_"+this.actionCard.count+"_png");
            this.addChild(this.cardobj);
            this.cardobj.x = this.btnContentBg.x - 10;
            this.cardobj.y = -10;
        }
        if (angang && gonggang && !canNotBu) {
            // var btn22:egret.Bitmap = MyLibrary.createBitmapByName("bububu_png");
            const gonggang = this.candoActions.find(can => can.actionType === 'GONG_GANG')
            if (gonggang.actionCards.length === 1) {
                let cardobj = new handPoker();
                cardobj.scaleX = cardobj.scaleY = 0.6;
                cardobj.setContent(gonggang.actionCards[0].suitType+"_"+gonggang.actionCards[0].count+"_png");
                this.addChild(cardobj);
                cardobj.x = btn22.x - 10;
                cardobj.y = -10;
            }
            const angang = this.candoActions.find(can => can.actionType === 'AN_GANG')
            if (angang.actionCards.length === 1) {
                let cardobj2 = new handPoker();
                cardobj2.scaleX = cardobj2.scaleY = 0.6;
                cardobj2.setContent(angang.actionCards[0].suitType+"_"+angang.actionCards[0].count+"_png");
                this.addChild(cardobj2);
                cardobj2.x = btn222.x - 10;
                cardobj2.y = -10;
            }
        }
        // if (angang && !canNotBu) {
        //     // var btn222:egret.Bitmap = MyLibrary.createBitmapByName("bububu_png");
        // }
        if(guo == false){
            this.btn_guo.alpha = 0;
            this.btn_guo.touchEnabled = false;
        }
        this.width = this.btnContentBg.width+111;
    }
}